package com.zhangc.blog.base.batchtask;

import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

@Component
public class BatchFactory implements InitializingBean {

    private ImmutableMap<String, BatchConvert> converMap = null;

    //
    public interface BatchConvert {
        void apply(BatchBean batchBean);
    }

    // 默认
    private static final BatchConvert defaultCovert = new BatchConvert() {
        @Override
        public void apply(BatchBean batchBean) {
        } // donothing
    };

    public BatchConvert getBatchConvert(String key) {
        return converMap.getOrDefault(key, defaultCovert);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, BatchConvert> map = Maps.newHashMap();
        map.put(BatchConstant.SKU, new SkuConvert());
        map.put(BatchConstant.SUPPLIER, new SupplierConvert());
        map.put(BatchConstant.PICTURE, new PictureConvert());
        converMap = ImmutableMap.copyOf(map);
    }

    class SkuConvert implements BatchConvert {

        @Override
        public void apply(BatchBean batchBean) {
            for (String skuNo : batchBean.getSkuNoSet()) {
                batchBean.getSkuMap().put(skuNo, "商品名称设置");
            }
        }
    }

    class SupplierConvert implements BatchConvert {

        @Override
        public void apply(BatchBean batchBean) {
            for (Tuple2<String, String> key : batchBean.getStoreSkuSet()) {
                batchBean.getSupplierMap().put(key, "供应商名称设置");
            }
        }
    }

    class PictureConvert implements BatchConvert {

        @Override
        public void apply(BatchBean batchBean) {
            Map<Tuple2<String, String>, String> supplierMap = batchBean.getSupplierMap();
            for (Tuple2<String, String> key : batchBean.getStoreSkuSet()) {
                batchBean.getPictureMap().put(key, supplierMap.get(key) + "后获取商品图片链接");
            }
        }
    }
}
